import { createStore } from 'vuex'
import api from '../api/index'
export default createStore({
  state: {
    playlist: [
      {
        al: { picUrl: 'http://p3.music.126.net/Qvs7rB8UdE-LBTGe689ycA==/72567767449545.jpg' },
        name: '翅膀',
        id: 394729,
        dt: 285336
      }
    ],
    playCurrentIndex: 0,
    isShow: false,
    playing: false, // 播放状态
    user: {
      isLogin: false,
      account: {},
      userDetail: {}
    },
    mvUrl: '',
    type: '热门作品',
    isactive: 0,
    isuser: null
  },
  mutations: {
    setPlaylist (state, value) {
      state.playlist = value
      state.playCurrentIndex = 0
    },
    upPlaylist (state, value) {
      state.playlist = value
    },
    pushPlaylist (state, value) {
      state.playlist.push(value)
    },
    setPlayIndex (state, value) {
      state.playCurrentIndex = value
    },
    setIsShow (state, value) {
      state.isShow = value
    },
    setPlaying (state, value) {
      state.playing = value
    },
    setUser (state, value) {
      state.user = value
    },
    setMvUrl (state, value) {
      state.mvUrl = value
    },
    setType (state, value) {
      state.type = value
    },
    setIsactive (state, value) {
      state.isactive = value
    },
    setIsUser (state, value) {
      state.isuser = value
    }
  },
  actions: {
    // 手机密码登录
    async phoneLogin (content, payload) {
      const result = await api.phoneLogin(payload.phone, payload.password)
      if (result.data.code === 200) {
        //   console.log(result)
        content.state.user.isLogin = true
        content.state.user.account = result.data.account
        const userDetail = await api.userDetail(result.data.account.id)
        content.state.user.userDetail = userDetail.data
        localStorage.userData = JSON.stringify(content.state.user)
        //  console.log(userDetail)
        content.commit('setUser', content.state.user)
        content.state.isuser = '登录成功'
      }
      //    console.log(result)
      return result
    },
    // 手机验证码登录
    async phoneLogins (content, payload) {
      const result = await api.phoneLogins(payload.phone, payload.captcha)
      console.log(result)
      if (result.data.code === 200) {
        console.log(result)
        content.state.user.isLogin = true
        content.state.user.account = result.data.account
        const userDetail = await api.userDetail(result.data.account.id)
        content.state.user.userDetail = userDetail.data
        localStorage.userData = JSON.stringify(content.state.user)
        console.log(userDetail)
        content.commit('setUser', content.state.user)
        content.state.isuser = '登录成功'
      }
      console.log(result)
      return result
    }
  },
  modules: {
  },
  getters: {

  }
})
